//
//  OrderNoticeVC.m
//  dyseller
//
//  Created by 红喇叭 on 2017/8/16.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "OrderNoticeVC.h"
#import "OrderNoticeCell.h"
#import "OrderListModel.h"
#import "PayOrderViewController.h"
#import "TableViewAnimationKit.h"

@interface OrderNoticeVC ()<UITableViewDelegate,UITableViewDataSource>{
    NSArray *_data;
}
@property(nonatomic,strong)UITableView *tableView;

@end

@implementation OrderNoticeVC

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
     [self setNavTitle:@"订单通知" titleColor:[UIColor blackColor] font:font17];
    self.view.backgroundColor = UIColorFromRGB(0xEBEBEB);
    self.tableView.backgroundColor = UIColorFromRGB(0xEBEBEB);
    _data = @[];
    [self loadingData];
    
    self.tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadingData];
        
    }];
    [self toreduce];
    //当然有评论推送过来时，刚好在评论页面，直接刷新
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(againLoading) name:@"news_OrderList" object:nil];
}
- (void)againLoading{
    [self.tableView.mj_header beginRefreshing];
}

//减少角标数字
- (void)toreduce{
    NSDictionary *dic = [userDefaults objectForKey:@"NocationList"];
    NSMutableArray *arr = dic.mutableCopy;
    
    NSMutableDictionary *newdic = ((NSDictionary *)arr[1]).mutableCopy;
    if([newdic[@"newItem"] intValue]>0){
    
        [[PPHttpRequsetData shareInstance] requestStyle:Request_POST URL:@"/ShopApi/Notice/unreadChange" isactive:YES parameter:@{@"number":@"0",@"flag":@"order"} successBlock:^(PPHttpRequest *request, id responseObj) {
            newdic[@"newItem"] = [NSString stringWithFormat:@"%d",[responseObj[@"order"] intValue]];
            arr[1] = newdic;
            [userDefaults setObject:arr forKey:@"NocationList"];
        } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
            [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
        }];
    }
}

- (void)loadingData{
    __weak typeof (self)weakself = self;
    [[PPHttpRequsetData shareInstance] requestStyle:Request_POST URL:@"/ShopApi/Notice/orderNotice" parameter:@{} successBlock:^(PPHttpRequest *request, id responseObj) {
        NSMutableArray *dataArray = @[].mutableCopy;
        [(NSArray *)responseObj enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            OrderListModel *mode = [OrderListModel yy_modelWithJSON:responseObj[idx]];
            [dataArray addObject:mode];
        }];
        _data = dataArray;
        [weakself.tableView reloadData];
         [weakself.tableView.mj_header endRefreshing];
        [TableViewAnimationKit toTopAnimationWithTableView:self.tableView];
    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
         [weakself.tableView.mj_header endRefreshing];
        [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
    }];
}



- (UITableView *)tableView{
    if(_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H-TabBarItemHeight-NavigationHeight+49) style:UITableViewStylePlain];

        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:@"OrderNoticeCell" bundle:nil] forCellReuseIdentifier:@"OrderNoticeCell"];
        
        //UITableView去掉自带系统的分割线
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OrderListModel *model = _data[indexPath.row];
    OrderNoticeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderNoticeCell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = UIColorFromRGB(0xEBEBEB);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.smallTime.text = [NSString stringWithFormat:@"%@",[ToolClass strToTime:model.add_time format:@"yyyy-MM-dd hh:mm:ss"]];
    cell.orderStyle.text = model.type;
    cell.orderNumber.text = [NSString stringWithFormat:@"%@",model.order_sn];
   // cell.money.text = [NSString stringWithFormat:@"消费:￥%@",dic[@"price"]];
    if([model.price isEqualToString:@"0.00"]){
        NSString *str = [NSString stringWithFormat:@"消费:%@紫喇叭",model.point_pay];
        cell.money.attributedText = [ToolClass AttributedString:str range:NSMakeRange(3,str.length-3) color:UIColorFromRGB(0xfd5974)];
    }else{
        cell.money.textColor = [UIColor blackColor];
        if([model.point_pay integerValue]>0){
        NSString *str = [NSString stringWithFormat:@"消费:￥%@+%@紫喇叭",model.price,model.point_pay];
            NSString *integral =  [NSString stringWithFormat:@"%@紫喇叭",model.point_pay];
            cell.money.attributedText = [ToolClass AttributedString:str range:NSMakeRange(str.length-integral.length,integral.length) color:UIColorFromRGB(0xfd5974)];
        }else{
            cell.money.text = [NSString stringWithFormat:@"消费:￥%@",model.price];
        }
    }

    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 130;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    OrderListModel *model = _data[indexPath.row];
    PayOrderViewController *vc = [[PayOrderViewController alloc] initWith:@[] style:1];
    vc.orderStr = model.order_sn;
    vc.fromVC = 1;//减角标的标记
    [self.navigationController pushViewController:vc animated:YES];
    

}

//- (void)loadUrl:(NSString *)orderID{
//    
//    __weak typeof (self)weakself = self;
//    [[PPHttpRequsetData shareInstance] requestStyle:Request_POST URL:@"/ShopApi/Order/orderList" parameter:@{@"keyWord":orderID} successBlock:^(PPHttpRequest *request, id responseObj) {
//        NSArray *payOrder = responseObj[@"payOrder"];
//        NSArray *isUsed = responseObj[@"goodsOrder"][@"isUsed"];
//        NSArray *noUsed = responseObj[@"goodsOrder"][@"noUsed"];
//        NSMutableArray *allArray = @[].mutableCopy;
//            [payOrder enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//                [allArray addObject:obj];
//            }];
//            
//            [isUsed enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//                [allArray addObject:obj];
//            }];
//            
//            [noUsed  enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//                [allArray addObject:obj];
//            }];
//        
//    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
//        [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
//    }];
//
//    
//}




@end
